﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;

namespace XToolkit.Drawing
{
    public static class ImageTool
    {
        /// <summary>
        /// 生成验证码图片，并写入指定的流中
        /// </summary>
        /// <param name="stream">要写入图片数据的流</param>
        /// <param name="validateCode">验证码</param>
        /// <param name="height">验证码图片的高度</param>
        public static void WriteValidateGraphic(Stream stream, string validateCode, int height = 22)
        {
            Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 14.0 + 10), height);
            Graphics g = Graphics.FromImage(image);
            try
            {
                //生成随机生成器   
                Random random = new Random();
                //清空图片背景色   
                g.Clear(Color.White);
                //画图片的干扰线   
                for (int i = 0; i < 10; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
                Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic));
                Color color1 = Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
                Color color2 = Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
                float f = (float)(random.Next(35, 90));
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
                 color1, color2, f, true);
                g.DrawString(validateCode, font, brush, 3, 2);
                //画图片的前景干扰点   
                for (int i = 0; i < 150; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                //画图片的边框线   
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                //保存图片数据   
                //MemoryStream stream = new MemoryStream();
                image.Save(stream, ImageFormat.Jpeg);
                //输出图片流   
                //return stream.ToArray();
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }

        /// <summary>   
        /// 创建验证码的图片   
        /// </summary>   
        /// <param name="validateCode">验证码</param>   
        /// <param name="height">生成的图片的高度</param>
        public static byte[] CreateValidateGraphic(string validateCode, int height = 22)
        {
            MemoryStream stream = new MemoryStream();
            WriteValidateGraphic(stream, validateCode, height);
            return stream.ToArray();
        }


        static Random r = new Random();
        /// <summary>
        /// 生成一个随机码
        /// </summary>
        /// <param name="length">要取得的验证码的长度</param>
        /// <returns></returns>
        private static string RandomCode(int length = 6)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                int x = r.Next(10);
                if (x >= 4)
                {
                    sb.Append((char)r.Next(65, 91));
                }
                else
                {
                    sb.Append(r.Next(10));
                }

            }
            return sb.ToString();
        }
    }
}
